﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App
{
    public partial class EditTeam : System.Web.UI.Page
    {
        AppLib.Team teamToEdit;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            teamToEdit = MidApp.ManageTeam.GetTeamByID(int.Parse(Server.HtmlEncode(Request.Cookies["teamID"].Value)));
            
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            TextBoxTeamName.Text = teamToEdit.TeamName;
            TextBoxLocation.Text = teamToEdit.Location;
            TextBoxDescription.Text = teamToEdit.Description;
            TextBoxTeamAvatarURL.Text = teamToEdit.ImageURL;

           RepeaterRole.DataSource = MidApp.ManageIsMember.GetAllMembers(teamToEdit.TeamID);
           RepeaterRole.DataBind();
                

        }

        protected void ButtonEditTeam_Click(object sender, EventArgs e)
        {
            LabelErrorMessage.Visible = true;
            if (teamToEdit.TeamName == TextBoxTeamName.Text || !MidApp.ManageTeam.TeamNameAlreadyTaken(TextBoxTeamName.Text))
            {
                LabelErrorMessage.Visible = false;

                teamToEdit.TeamName = TextBoxTeamName.Text;
                teamToEdit.Location = TextBoxLocation.Text;
                teamToEdit.Description = TextBoxDescription.Text;
                teamToEdit.ImageURL = TextBoxTeamAvatarURL.Text;

                MidApp.ManageTeam.AssignNewTeamInfo(teamToEdit);

                Label3.Text = "Změna údajů uložena";
                Label3.Visible = true;             
            }     

        }

        protected void ConfirmLeadership_Click(object sender, EventArgs e)
        {
            var aux = int.Parse(((LinkButton)sender).CommandArgument);
            MidApp.ManageIsMember.SetLeader(aux, teamToEdit.TeamID, true);

        }
        protected void RemoveLeadership_Click(object sender, EventArgs e)
        {
            var aux = int.Parse(((LinkButton)sender).CommandArgument);
            MidApp.ManageIsMember.SetLeader(aux, teamToEdit.TeamID, false);

            if (aux == Common.GetLoggedUser(this.Page).UserID)
                Response.Redirect("Default.aspx");
        }
        protected void ConfirmTreasurer_Click(object sender, EventArgs e)
        {
            var aux = int.Parse(((LinkButton)sender).CommandArgument);

            MidApp.ManageIsMember.SetTreasurer(aux, teamToEdit.TeamID,true);
        }

        protected void RemoveTreasurer_Click(object sender, EventArgs e)
        {
            var aux = int.Parse(((LinkButton)sender).CommandArgument);

            MidApp.ManageIsMember.SetTreasurer(aux,teamToEdit.TeamID,false);
        }

        //metoda vraci string hodnotu ID clena
        protected string GetMemberID(object sender)
        {
            var aux = (AppLib.IsMember)sender;

            return aux.UserID.ToString();
        }
        //metoda vraci nick clena tymu
        protected string GetMemberNick(object sender)
        {
            var aux = (AppLib.IsMember)sender;

            return MidApp.ManageUser.GetUserByID(aux.UserID).Nick;
        }
        //obsluha tlacitka pro zobrazeni detailu hrace
        protected void LinkButtonUserInfo_Click(object sender, EventArgs e)
        {
            Response.Cookies["UserDetail"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("UserDetail.aspx");
        }
        protected string GetMemberAvatar(object sender)
        {
            var pom = (AppLib.IsMember)sender;
            return MidApp.ManageUser.GetUserImage(pom.UserID);
        }
        protected string GetMemberEmail(object sender)
        {
            var pom = (AppLib.IsMember)sender;
            return (MidApp.ManageUser.GetUserByID(pom.UserID)).PrimaryEmail;
        }
        //rozhoduje, zda hrac neni psoeldnim vedoucim, posledni vedouci nejde odebrat
        protected bool EnableToDemote()
        {
            var aux = MidApp.ManageIsMember.GetAllLeaders(teamToEdit.TeamID).Count;
            
            bool boo=true;

            if (aux == 1)
                boo = false;

            return boo;
        }

    }
}